package com.tbit.uqbike.client.pojo;

import com.tbit.uqbike.client.constant.DisPatchConstant;
import com.tbit.uqbike.client.util.NonnullSet;
import lombok.Data;

import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
 * 还车调度
 *
 * @author Leon
 * 2019年10月16日 下午7:56:27
 */
@Data
public class Dispatch {
    /**
     * 调度类型
     * 1：在还车点内
     * 2：不支持调度不在还车点
     * 3：支持调度不在运营区域
     * 4：支持调度不在还车点
     * 5：头盔不在位<br>
     * FIXME 兼容未升级的小程序，后续全平台小程序升级后删除，仅保留types字段
     */
    private Integer type;
    /**
     * 第二种调度类型，主要是用于组合收取调度费用<br>
     * FIXME 兼容未升级的小程序，后续全平台小程序升级后删除，仅保留types字段
     */
    private Integer secondType;
    /**
     * 调度类型列表
     * 包含上面的type和secondType
     */
    private Set<SubDispatch> types = new NonnullSet<>();
    /**
     * 调度费用
     */
    private Double money;
    /**
     * 头盔锁罚款费用
     */
    private Integer helmetMoney;
    /**
     * 减免费用
     */
    private Integer reduceMoney;
    /**
     * 站点角度
     */
    private Double parkAngle;
    /**
     * 终端角度
     */
    private Double machineAngle;

    /**
     * 是否学习豁免
     * */
    private boolean studyToReduceDispatch = false;

    public Dispatch() {
        super();
    }

    public Dispatch(Integer type, Double money) {
        super();
        this.type = type;
        this.money = money;
        addType(type, Optional.ofNullable(money).orElse(0d));
    }

    public Dispatch(Integer type, Double money, Integer helmetMoney, Integer secondType) {
        this.type = type;
        this.money = money;
        this.helmetMoney = helmetMoney;
        this.secondType = secondType;
        addType(type,Optional.ofNullable(money).orElse(0d));
        addType(secondType,Optional.ofNullable(helmetMoney).orElse(0).doubleValue());
    }

    public Dispatch(Integer type, Double money, Integer helmetMoney, Integer reduceMoney,
                    Double parkAngle, Double machineAngle) {
        this.type = type;
        this.money = money;
        this.helmetMoney = helmetMoney;
        this.reduceMoney = reduceMoney;
        this.parkAngle = parkAngle;
        this.machineAngle = machineAngle;
        addType(type,Optional.ofNullable(money).orElse(0d));
    }

    public void setType(Integer type) {
        this.type = type;
        addType(type,0d);
    }

    public void setSecondType(Integer secondType) {
        this.secondType = secondType;
        addType(secondType,Optional.ofNullable(helmetMoney).orElse(0).doubleValue());
    }

    /**
     * 添加调度类型
     */
    public boolean addType(Integer type,Double money) {
        if (type == null) {
            return false;
        }
        return this.types.add(new SubDispatch(type, money));
    }


    public boolean containsType(Integer type) {
        return types.contains(new SubDispatch(type, null));
    }

    public void removeType(Integer type) {
        types.remove(new SubDispatch(type, null));
    }

    public void setReduceMoneyNew(Integer reduceMoney){
        this.reduceMoney = reduceMoney;
        // 站外、超区调度费豁免 除了头盔费用，其他都减免
        for (SubDispatch subDispatch : types) {
            if ((!Objects.equals(subDispatch.getType(), DisPatchConstant.DISPATCH_HELMET) || !Objects.equals(subDispatch.getType(), DisPatchConstant.DISPATCH_HELMET_EX)) && subDispatch.getMoney() > 0d){
                subDispatch.setMoney(subDispatch.getMoney() - reduceMoney);
                break;
            }
        }
    }
}
